package com.parkinglot.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component

public class HttpUrlutils {
    @Async
    public Map<String, Object> getresult(String url) {
        StringBuilder sb = new StringBuilder();
        String a = null;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            //开始连接
            conn.connect();
            System.out.println("连接成功");
            System.out.println(conn.getResponseCode() + "-------------------------");


            // 请求返回的数据
            InputStream in = conn.getInputStream();

            try {
                byte[] data1 = new byte[in.available()];
                in.read(data1);
                // 转成字符串
                a = new String(data1);
                map = (Map) JSON.parse(a);
                map.put("status", "success");
                System.out.println(map);
                log.info(map.toString());
                in.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        } catch (Exception e) {
            log.warn("异常对象:{},异常消息:{},异常栈信息:{}", e.getClass(), e.getMessage());
            e.printStackTrace();

        }
        return map;
    }
    public Map<String, Object> getmonthvip(String url,String amount_receivable,String begin_time,String car_number,String end_time,String parkId) {
        StringBuilder sb = new StringBuilder();
        String a = null;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            //开始连接
            conn.connect();
            DataOutputStream out = new DataOutputStream(conn
                    .getOutputStream());
            System.out.println("连接成功");
            System.out.println(conn.getResponseCode() + "-------------------------");
            String parm="amount_receivable="+amount_receivable+"&amount_receivable="+amount_receivable+"&car_number="+car_number+"&end_time="+end_time+"&parkId="+parkId;
            out.writeBytes(parm);
            out.flush();
            out.close();
            // 请求返回的数据
            InputStream in = conn.getInputStream();

            try {
                byte[] data1 = new byte[in.available()];
                in.read(data1);
                // 转成字符串
                a = new String(data1);
                map = (Map) JSON.parse(a);
                map.put("status", "success");
                System.out.println(map);
                log.info(map.toString());
                in.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        } catch (Exception e) {
            log.warn("异常对象:{},异常消息:{},异常栈信息:{}", e.getClass(), e.getMessage());
            e.printStackTrace();

        }
        return map;
    }


    public Map<String, Object> getresultty(String url) {
        StringBuilder sb = new StringBuilder();
        String a = null;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            //开始连接
            conn.connect();
            System.out.println(conn.getResponseCode() + "-------------------------");
            if (conn.getResponseCode() == 200) {
                System.out.println("连接成功");
                // 请求返回的数据
                InputStream in = conn.getInputStream();

                try {
                    byte[] data1 = new byte[in.available()];
                    in.read(data1);
                    // 转成字符串
                    a = new String(data1);
                    map = (Map) JSON.parse(a);
                    map.put("status", "success");
                    System.out.println(map);
                    log.info(map.toString());
                    in.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            log.warn("异常对象:{},异常消息:{},异常栈信息:{}", e.getClass(), e.getMessage());
            e.printStackTrace();

        }
        return map;
    }

}
